function payAli(rs_data) {
  location.href = "https://openapi.alipay.com/gateway.do?" + rs_data;
}
function payQQ(vm, rs_data, order_id, sign_up_id, token, getOrder) {
  mqq.tenpay.pay({
    tokenId: rs_data.tokenId,
  }, function (result, resultCode) {
    var params = {};
    params.order_id = order_id;
    params.sign_up_id = sign_up_id;
    params.token = token;
    params.user_id = vm.signUpInfo.user_id;
    api.post(getOrder, params)
      .then(function (response) {
        var rs_data = response.data.data;
        if (response.data.res_code == 1) {
          // vm.getUserInfo();
          vm.paySuccess = true;
          vm.mode = true;
          setTimeout(function () {
            vm.paySuccess = false;
            vm.mode = false;
          }, 1000);
          //  TODO exam_id
          location.replace("/tlmall/order_list");
        } else {
          vm.mode = true;
          vm.payFail = true;
          // alert('支付失败,请刷新页面重试！');
          setLog(getOrder + ':fail', JSON.stringify(rs_data));
        }
        vm.submiting = false;
      })
      .catch(function (error) {
        alert('支付超时,请刷新页面重试！');
        setLog(getOrder + ':catch', JSON.stringify(error));
        vm.submiting = false;
      });
  });
}
function payWX(vm, rs_data, order_id, sign_up_id, token, getOrder) {
  var appId = "";
  var timeStamp = "";
  var nonceStr = "";
  var package = "";
  var signType = "";
  var paySign = "";
  function onBridgeReady() {
    WeixinJSBridge.invoke(
      'getBrandWCPayRequest', {
        "appId": appId,     //公众号名称，由商户传入     
        "timeStamp": timeStamp,         //时间戳，自1970年以来的秒数     
        "nonceStr": nonceStr, //随机串     
        "package": package,
        "signType": signType,         //微信签名方式：     
        "paySign": paySign //微信签名 
      },
      function (res) {
        var params = {};
        params.order_id = order_id;
        params.sign_up_id = sign_up_id;
        params.token = token;
        params.user_id = vm.signUpInfo.user_id;
        api.post(getOrder, params).then(function (response) {
          var rs_data = response.data.data;
          if (response.data.res_code == 1) {
            // vm.getUserInfo();
            vm.paySuccess = true;
            vm.mode = true;
            setTimeout(function () {
              vm.mode = true;
              vm.paySuccess = false;
            }, 1000);
            location.replace("/tlmall/order_list");
          } else {
            vm.payFail = true;
            vm.mode = true;
            // alert('支付失败,请刷新页面重试！');
            setLog(getOrder + ':fail', JSON.stringify(rs_data));
          }
          vm.submiting = false;
        }).catch(function (error) {
          alert('支付超时,请刷新页面重试！');
          setLog(getOrder + ':catch', JSON.stringify(error));
          vm.submiting = false;
        });
      }
    );
  }
  appId = rs_data.appId;
  timeStamp = rs_data.timeStamp;
  nonceStr = rs_data.nonceStr;
  package = rs_data.package;
  signType = rs_data.signType;
  paySign = rs_data.paySign;

  if (typeof WeixinJSBridge == "undefined") {
    if (document.addEventListener) {
      document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
    } else if (document.attachEvent) {
      document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
      document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
    }
  } else {
    onBridgeReady();
  }
}

function submit(vm, url, getOrder) {
  api.post(url, vm.signUpInfo)
    .then(function (res) {
      if (res.data.res_code == 1) {
        var rs_data = res.data.data;
        var token = vm.signUpInfo.token;
        switch (vm.signUpInfo.pay_type) {
          case 1:
            payWX(vm, rs_data.pay_params, rs_data.order_id, rs_data.sign_up_id, token, getOrder);
            break;
          case 2:
            payAli(rs_data.pay_params, rs_data.order_id, rs_data.sign_up_id, token);
            break;
          case 3:
            payQQ(vm, rs_data.pay_params, rs_data.order_id, rs_data.sign_up_id, token, getOrder);
            break;
        }
      } else {
        vm.submiting = false;
        alert('支付失败，请关闭网页后重新尝试！');
        setLog(url + ':fail', JSON.stringify(res.data));
      }
    }).catch(function (error) {
      alert('支付超时，请刷新页面重试！');
      setLog(url + ':catch', JSON.stringify(error));
      vm.submiting = false;
    });
}